package com.android.openapp.data;



import org.json.JSONArray;
import org.json.JSONObject;


import android.graphics.Color;
import android.util.Log;

import com.socrata.api.HttpConnection;

import com.socrata.data.View.Row;

public class Agriturismo extends Place {
	
	
	
	public Agriturismo(Row agri) {
		super.setMetadata(Place.listmetadati.get(3));
		super.setRow(agri);
		setBcolor(Color.LTGRAY);
		super.setMapData(super.getRow().getDataFieldsForSerialization());
		/*
		Address a;
		try {
			Log.d("Agriturismo", Boolean.toString(mGeoCoder.isPresent()));
			Log.d("Agriturismo", this.getAddress());
			List<Address> al=new ArrayList<Address>();
			
			//al = mGeoCoder.getFromLocationName(this.getAddress(), 1);
			al = mGeoCoder.getFromLocationName("", 1);
			a=al.get(0);
			Log.v("place","Geocoding "+this.getAddress());
			super.setCoord(a.getLatitude(), a.getLongitude());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		
		Double lon = new Double(0);
        Double lat = new Double(0);

        try {
        	
        	
            
            JSONObject jsonObject=HttpConnection.getLocationInfo(getAddress());
            
			lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lng");

            lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
                .getJSONObject("geometry").getJSONObject("location")
                .getDouble("lat");

        } catch (Exception e) {
            e.printStackTrace();

        }
        super.setCoord(lat, lon);
		
	}
	
	@Override
	public String getDenominazione() {
		return super.getStringValue("NOME_AGRITURISMO");
	}

	@Override
	public String getTipologia() {
		if(getStringValue("PRODOTTO") != null)
			return "Specialita': "+super.getStringValue("PRODOTTO");
		return "Specialita': non definita";
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getAddress() {
		StringBuilder builder = new StringBuilder();
		
		if (getStringValue("INDIRIZZO_COMPLETO") != null){
			builder.append(getStringValue("INDIRIZZO_COMPLETO")).append(", ");
		}
			builder.append(getStringValue("CAP_COM_PRO"));
		return builder.toString();
	}

	@Override
	public String getContactInfo() {
		StringBuilder builder = new StringBuilder();
		if(getStringValue("TEL1") != null)
			builder.append("Tel.\n").append(getStringValue("TEL1"));
		if(getStringValue("TEL2") != null)
			builder.append(" - ").append(getStringValue("TEL2"));
		builder.append("\n");
		
		if(getStringValue("SITO") != null)
			builder.append("Sito web\n").append(getStringValue("SITO")).append("\n");
		if(getStringValue("EMAIL") != null)
			builder.append("Email\n").append(getStringValue("EMAIL"));
		
		return builder.toString();
	}

}
